<template>
  <el-dialog
    class='subject-status-add-record'
    :title="dialogTitle"
    width="600px"
    :show-close='true'
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="handleClose">
    <div>
      <el-form
        :rules="rules"
        ref="formRef"
        size="mini"
        :model="formData"
        label-width="140px">
        <el-form-item label="编码值：" prop='codeValue'>
          <el-input
            style="width: 80%"
            clearable
            placeholder="请输入"
            maxlength="30"
            v-model.trim="formData.codeValue">
          </el-input>
        </el-form-item>
        <el-form-item label="状态名称：" prop='statusName'>
          <el-input
            style="width: 80%"
            clearable
            placeholder="请输入"
            maxlength="30"
            v-model="formData.statusName">
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading='loading' size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    editData: Object,
    dialogVisible: Boolean,
    dialogTitle: String,
    projectId: String
  },
  data () {
    return {
      rules: {
        codeValue: [{ required: true, message: '请输入', trigger: 'blur' }],
        statusName: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      loading: false,
      formData: {}
    }
  },

  watch: {
    dialogVisible(val) {
      if (val) {
        this.formData = this.editData.id ? { ...this.editData } : {}
      } else {
        this.formData = {}
      }
    },
  },

  methods: {
    ...mapActions('subjectStatus', ['addSubjectStatus', 'editSubjectStatus']),
    handleClose () {
      this.$emit('handleCancel')
    },

    handleSave () {
      this.loading = true
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          const data = {
            ...this.formData,
            status: 1, // 状态为1表示为手动添加
            projectId: this.projectId
          }
          if (data.id) {
            this.editSubjectStatus({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message.success('保存成功')
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          } else {
            this.addSubjectStatus({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message.success('保存成功')
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },

  }
}
</script>
<style lang="scss" scoped>
  @import './add-record.scss';
</style>
